<?php
include_once('includes/classes/actions.php');
include_once('includes/classes/league.php');
class Leagues extends Actions {
	function action($post_vars) {
		global $session;
		
		$return_str = "";
		if (!$session->isAdmin()) 
			$return_str .= parent::soon($post_vars, "yetersiz yetki");
		else {
			switch ($post_vars['action']) {
				default:
				case "Form":
					$return_str .= $this->Form($post_vars);
					$return_str .= $this->View ($post_vars);
					break;
				case "Delete":
					$return_str .= $this->Delete ($post_vars);
					$return_str .= $this->View ($post_vars);				
					break;
				case "Update":
					$return_str .= $this->Update ($post_vars);
					$return_str .= $this->View ($post_vars);				
					break;
				case "Add":
					$return_str .= $this->Add ($post_vars);
					$return_str .= $this->View ($post_vars);
					break;
				case "Soon":
					$return_str .= $this->Soon ($post_vars);
					$return_str .= $this->View ($post_vars);
					break;				
			}
		}
		return $return_str;
	}
	function Validate ($post_vars) {
		extract ($post_vars);	
		if ($league == "")
			return "Lig ismi eksik.";
		if ($season == "")
			return "Sezon bos birakilmis.";
		
	}	
	//-----------------------------------------
	function Form ($post_vars=NULL, $error=NULL) {
		require_once("ui/init.php");
		$smarty = new _Smarty();
		$smarty->assign ($post_vars);
		if ($error != NULL)
			$smarty->assign ("error", $error);
		return $smarty->fetch ('leaguesForm.html');
	}	
	//-----------------------------------------
	function View ($post_vars) {
		global $database;
		//extract ($post_vars);
		$leagues = array();
		require_once("ui/init.php");
		$smarty = new _Smarty ();
		foreach ($database->getLeagues() as $row) {
			$league = new League($row["id"], $row["league"], $row["season"]);
			$smarty->append('leagues', $league);
		}	
		return $smarty->fetch ('leagueList.html');
	
	}	
	//-----------------------------------------
	function Add ($post_vars) {
		require_once("ui/init.php");
		$smarty = new _Smarty ();
		
		$error = $this->Validate ($post_vars);
		
		if ($error != "") {
			return $this->Form ($post_vars, $error);
		} else {
			$globals = new Globals();
			$post_vars = $globals->AddSlashArray ($post_vars);
			extract ($post_vars);
			$league = new League(0, $league, $season);
			if (!$league->Add()) 
				$smarty->assign ("error", "Couldn't add $first $last to database.");	
			return $this->Form($post_vars);
		}
	}	
	//-----------------------------------------
	function Delete ($post_vars) {
		require_once("ui/init.php");
		$smarty = new _Smarty ();
			
		$globals = new Globals();
		$post_vars = $globals->AddSlashArray ($post_vars);
		extract ($post_vars);
		$league = new League($id, "", "");
		if (!$league->Delete()) 
			$smarty->assign ("error", "Couldn't add $first $last to database.");	
		return $this->Form($post_vars);
	
	}		
	
	//-----------------------------------------
	function Update ($post_vars) {
		require_once("ui/init.php");
		$smarty = new _Smarty ();
		
		$error = $this->Validate ($post_vars);
		
		if ($error != "") {
			return $this->Form ($post_vars, $error);
		} else {
			$globals = new Globals();
			$post_vars = $globals->AddSlashArray ($post_vars);
			extract ($post_vars);
			$league = new League($id, $league, $season);
			if (!$league->Update()) 
				$smarty->assign ("error", "Couldn't add $first $last to database.");	
			return $this->Form();
		}
	}		
}
?>